package com.xywl.escort.view.address;

import com.xywl.escort.bean.AddrReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/3/7.
 */

public class AddAddressPresenter implements AddAddressContract.Presenter {
    private AddAddressContract.View mView;
    private ApiServer mApiServer;

    @Inject
    AddAddressPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        Timber.tag("AddAddressPresenter").e("unSubscribe");
        mView = null;
    }

    @Override
    public void takeView(AddAddressContract.View view) {
        this.mView = view;
    }

    @Override
    public void addUserShippingAddress(AddrReq req) {
        mApiServer.addrAdd(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.addAddressFailure("添加收货地址失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.addAddressSuccess(resp);
                            } else {
                                mView.addAddressFailure(resp.msg);
                            }
                        } else {
                            mView.addAddressFailure("添加收货地址失败");
                        }
                    }
                });
    }

    @Override
    public void updateUserShippingAddress(AddrReq req) {
        mApiServer.addrUpdate(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.updateAddressFailure("修改收货地址失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.updateAddressSuccess(resp);
                            } else {
                                mView.updateAddressFailure(resp.msg);
                            }
                        } else {
                            mView.updateAddressFailure("修改收货地址失败");
                        }
                    }
                });
    }
}
